package com.it.controller;

import cn.dev33.satoken.util.SaResult;
import com.github.pagehelper.PageInfo;
import com.it.bean.Dict;
import com.it.page.PageImpl;
import com.it.page.PageQuery;
import com.it.service.DictService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequiredArgsConstructor
@RequestMapping("/dict")
public class DictController {

    private final DictService dictService;

    @GetMapping("/single/{field}")
    public SaResult single(@PathVariable("field") String field) {
        List<Dict> ds =  dictService.single(field);
        return SaResult.data(ds);
    }

    @PostMapping("/many")
    public SaResult many(@RequestBody List<String> fields) {
        Map<String,List<Dict>> dataMap = dictService.many(fields);
        return SaResult.data(dataMap);
    }

    @PostMapping("/page")
    public SaResult page(@RequestBody PageQuery<Dict> pageQuery) {
        PageInfo<Dict> pageInfo = dictService.page(pageQuery);
        return SaResult.data(PageImpl.of(pageInfo));
    }

    @PostMapping("/delete/{id}")
    public SaResult del(@PathVariable("id") Long id) {
        dictService.delete(id);
        return SaResult.ok();
    }

    @PostMapping("/save")
    public SaResult save(@RequestBody Dict dict) {
        dictService.save(dict);
        return SaResult.ok();
    }
}
